var dict = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"];
/* function randomcolor(){
	
	var c = Math.floor(Math.random() * 16);
	return dict[c];
}

function randomcolor_3(){
	return '#'+randomcolor()+randomcolor()+randomcolor();
}

function randomcolor_6(){
	return '#'+randomcolor()+randomcolor()+randomcolor()+randomcolor()+randomcolor()+randomcolor();
} */

function randomcolor_6(){
	var r = '#';
	for(var i=0;i<3;i++){
		var s1 = DectoHex(Math.floor(Math.random()*256));
		if(s1.length == 3){
			r+='0';
			
		}
		r+=s1.substr(2);
	}
	return r;
}

function init_color(circle){
	var r = '#';
	for(var i=0;i<3;i++){
		var c = Math.floor(Math.random()*256);
		circle.colornum[i] = c;
		var s1 = DectoHex(c);
		if(s1.length == 3){
			r+='0';
			
		}
		r+=s1.substr(2);
	}
	circle.color = r;
}

function gradual_color(circle){
	var r = '#';
	for(var i= 1;i<circle.color.length;){
		
		var s = '0x'+circle.color[i++]+circle.color[i++];
		
		var ii = parseInt(s);
		var g = circle.gradual[((i-1)/2-1)%3];
		ii += g;
		
		if(ii <= Math.abs(g) || ii >= (255 - Math.abs(g))){
			circle.gradual[((i-1)/2-1)%3] *= -1;
		}
		var n = Date.now();
		var s1 = DectoHex(ii);
		//console.log("spend : "+(Date.now() - n))
		if(s1.length == 3){
			r+='0';
			
		}
		r+=s1.substr(2);
	}
	circle.color = r;
}

function gradual_color2(circle){
	var r = '#';
	//red green blue
	for (var i = 0; i < circle.colornum.length; i++) {
		circle.colornum[i] += circle.gradual[i];
		if(circle.colornum[i] <= Math.abs(circle.gradual[i]) || circle.colornum[i] >= (255 - Math.abs(circle.gradual[i]))){
			circle.gradual[i] *= -1;
		}
		var s1 = DectoHex(circle.colornum[i]);
		if(s1.length == 3){
			r+='0';
			
		}
		r+=s1.substr(2);
	}
	
	circle.color = r;
}

/* function spend_print(f){
	var n = Data.now();
	eval(f)
	console.log("spend : "+(Date.now() - n))
} */

function DectoHex(dec)
{
    var C = 0xF;    // Check 掩码，和dec相与取出dec的4bit
    var A = dec;      // Accumulate
    var B = -1;     // Base string length
    var S = '';     // Source 'string'
    var hex = '0x';   // 转换后的字符串
    do
    {
        ++B;        //计算十六进制有几位
        A&=C;     //每次取目标值的4bit

        switch(A)   //如果十进制数值大于10，需要转成十六进制A,B,C,D,E,F
        {
            case 0xA:
                A='A';
                break;
            case 0xB:
                A='B';
                break;
            case 0xC:
                A='C';
                break;
            case 0xD:
                A='D';
                break;
            case 0xE:
                A='E';
                break;
            case 0xF:
                A='F';
                break;
                A=(A);
        }
        S+=A;       

        dec>>>=0x04;         //十进制数右移4bit，左边移入0，转成新的十六进制数
        A=dec;
    }while(dec);                 //如果十进制右移后变成0，表示十进制数已经处理完成
    
    do
    {
        hex+=S[B];                //将处理完的十六进制按照倒序填入返回的字符串
        
    }while(B--);

    S = B = A = C = dec; // 已经不用的变量都初始化为0.
    
    return hex;
}